package com.polaris.live.im.db.dao

import androidx.room.*
import com.polaris.live.im.db.entity.PurchasePretreatmentEntity

/**
 * PurchasePretreatmentEntityDao
 *
 * @author Created by lucas on 2023/11/23 10:47
 * @since 1.0.0
 */
@Dao
interface PurchasePretreatmentEntityDao {

    /**
     * 保存
     */
    @Insert
    suspend fun save(entity: PurchasePretreatmentEntity): Long

    /**
     * 根据token删除
     */
    @Query("delete from purchase_pretreatment where token = :token")
    fun delete(token: String)

    /**
     * 查询最近的一条包含此商品的预存信息
     */
    @Query("select * from purchase_pretreatment where account_id = :accountId and product_id = :productId order by timestamp desc limit 1")
    suspend fun findByAccountIdAndProductId(accountId: String, productId: String): PurchasePretreatmentEntity?

    /**
     * 清理冗余数据
     */
    @Query("delete from purchase_order where timestamp < :timestamp")
    suspend fun clearRedundancy(timestamp: Long)

}